package com.coursehub.course.entity;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 课程实体类
 * 
 * @author CourseHub Team
 */
@Entity
@Table(name = "courses")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Course {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @NotBlank(message = "课程名称不能为空")
    @Size(max = 100, message = "课程名称长度不能超过100个字符")
    @Column(nullable = false, length = 100)
    private String name;
    
    @Size(max = 500, message = "课程描述长度不能超过500个字符")
    @Column(length = 500)
    private String description;
    
    @NotNull(message = "教师ID不能为空")
    @Column(name = "teacher_id", nullable = false)
    private Long teacherId;
    
    @Column(name = "teacher_name", length = 50)
    private String teacherName;
    
    @Size(max = 20, message = "课程代码长度不能超过20个字符")
    @Column(name = "course_code", unique = true, length = 20)
    private String courseCode;
    
    @Column(name = "max_students")
    private Integer maxStudents;
    
    @Column(name = "current_students")
    private Integer currentStudents = 0;
    
    @Enumerated(EnumType.STRING)
    @Column(nullable = false)
    private CourseStatus status = CourseStatus.DRAFT;
    
    @Column(name = "start_date")
    private LocalDateTime startDate;
    
    @Column(name = "end_date")
    private LocalDateTime endDate;
    
    @Column(name = "created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;
    
    @Column(name = "updated_at")
    private LocalDateTime updatedAt;
    
    @OneToMany(mappedBy = "course", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<Chapter> chapters;
    
    @PrePersist
    protected void onCreate() {
        createdAt = LocalDateTime.now();
        updatedAt = LocalDateTime.now();
    }
    
    @PreUpdate
    protected void onUpdate() {
        updatedAt = LocalDateTime.now();
    }
    
    /**
     * 课程状态枚举
     */
    public enum CourseStatus {
        DRAFT("草稿"),
        PUBLISHED("已发布"),
        ONGOING("进行中"),
        COMPLETED("已完成"),
        ARCHIVED("已归档");
        
        private final String description;
        
        CourseStatus(String description) {
            this.description = description;
        }
        
        public String getDescription() {
            return description;
        }
    }
}